﻿<%@ Page Language="C#" %>
function createWeather(divObj, id, parentObj, pRefObj) {
    var tblObj = parentObj.createElement('TABLE');
    tblObj.id = id;
    tblObj.width = pRefObj.width-pRefObj.iconWidth;
    tblObj.height = "170";
    tblObj.border = "0";
    tblObj.cellSpacing = "0";
    tblObj.cellPadding = "0";

    // 第1天按钮对象引用
    var tdBtn1Obj = null;
    // 第2天按钮对象引用
    var tdBtn2Obj = null;
    // 第3天按钮对象引用
    var tdBtn3Obj = null;
    // 天气对象引用
    var divWeatherObj = null;
    // 风向对象引用
    var divWindObj = null;
    // 第1张图片
    var pRefImg1Obj = null;
    // 第2张图片
    var pRefImg2Obj = null;
    // 温度控件对象引用
    var pRefTemperatureObj = null;
    // 天气控件对象引用
    var pRefWeatherObj = null;
    // 城市名称对象引用
    var pRefCityObj = null;

    var tdRow = tblObj.insertRow(-1);
    var tdCell = tdRow.insertCell(-1);
    tdCell.width = pRefObj.width-pRefObj.iconWidth;
    tdCell.background = "../../images/sunbg.jpg";
    tdCell.vAlign = "top";

    // 天气图片
    var tblImgObj = parentObj.createElement('TABLE');
    tblImgObj.border = "0";
    tblImgObj.cellSpacing = "0";
    tblImgObj.cellPadding = "0";
    tblImgObj.width = "100%";

    var tbImgRow = tblImgObj.insertRow(-1);
    var tbImgCell = tbImgRow.insertCell(-1);
    tbImgCell.width = "65";
    var imgObj = parentObj.createElement("IMG");
    imgObj.src = "../../images/weather/none.jpg";
    tbImgCell.appendChild(imgObj);
    pRefImg1Obj = imgObj;

    tbImgCell = tbImgRow.insertCell(-1);
    tbImgCell.width = "65";
    imgObj = parentObj.createElement("IMG");
    imgObj.src = "../../images/weather/none.jpg";
    tbImgCell.appendChild(imgObj);
    pRefImg2Obj = imgObj;

    tbImgCell = tbImgRow.insertCell(-1);
    tbImgCell.align = "right";
    tbImgCell.style.fontSize = "20px";
    tbImgCell.style.fontFamily = "黑体";
    tbImgCell.style.paddingRight = "8px";
    // tbImgCell.appendChild( parentObj.createTextNode(shWF.name) );
    // tbImgCell.title = shWF.description;
    // 设置城市名称对象引用
    pRefCityObj = tbImgCell;
    
    // 温度区域
    var tblTextObj = parentObj.createElement('TABLE');
    tblTextObj.border = "0";
    tblTextObj.cellSpacing = "0";
    tblTextObj.cellPadding = "0";
    tblTextObj.width = "100%";
    var tbTextRow = tblTextObj.insertRow(-1);
    var tbTextCell = tbTextRow.insertCell(-1);
    tbTextCell.align = "center";

    // 温度区域 - 插入天气情况
    divWeatherObj = parentObj.createElement('DIV');
    divWeatherObj.style.fontSize = "14px";
    tbTextCell.appendChild( divWeatherObj );

    // 温度区域 - 插入风向情况
    divWindObj = parentObj.createElement('DIV');
    divWindObj.style.fontSize = "9px";
    tbTextCell.appendChild( divWindObj );

    // 温度区域 - 插入温度情况
    tbTextCell = tbTextRow.insertCell(-1);
    tbTextCell.align = "center";
    tbTextCell.style.fontSize = "22px";
    tbTextCell.style.fontFamily = "黑体";
    tbTextCell.style.fontWeight = "900";
    // 设置温度控件对象引用
    pRefTemperatureObj = tbTextCell;
    
    // 按钮区域
    var tblBtnObj = parentObj.createElement('TABLE');
    tblBtnObj.border = "0";
    tblBtnObj.cellSpacing = "0";
    tblBtnObj.cellPadding = "0";
    tblBtnObj.align = "center";
    //tblBtnObj.width = "100%";
    tblBtnObj.height = "61";

    var tbBtnRow = tblBtnObj.insertRow(-1);
    
    // 创建第1天按钮
    var tdBtn1Obj = tbBtnRow.insertCell(-1);
    tdBtn1Obj.id = pRefObj.id + "Btn1";
    tdBtn1Obj.align = "center";
    tdBtn1Obj.vAlign = "bottom";
    tdBtn1Obj.width = "52";
    tdBtn1Obj.style.cursor = "hand";
    // 设置第1天阳光指数
    // tdBtn1Obj.title = shWF.weather[0].sunshine + "\r\n\r\n" + shWF.weather[0].detail;

    tbBtnCell = tbBtnRow.insertCell(-1);
    tbBtnCell.width = "20";
    
    // 创建第2天按钮
    tdBtn2Obj = tbBtnRow.insertCell(-1);
    tdBtn2Obj.align = "center";
    tdBtn2Obj.vAlign = "bottom";
    tdBtn2Obj.width = "52";
    tdBtn2Obj.style.cursor = "hand";

    tbBtnCell = tbBtnRow.insertCell(-1);
    tbBtnCell.width = "20";

    // 创建第3天按钮
    tdBtn3Obj = tbBtnRow.insertCell(-1);
    tdBtn3Obj.align = "center";
    tdBtn3Obj.vAlign = "bottom";
    tdBtn3Obj.width = "52";
    tdBtn3Obj.style.cursor = "hand";

    // 画出第1天的天气情况
    // redrawWeather(shWF, pRefObj.showIndex, tdBtn1Obj, tdBtn2Obj, tdBtn3Obj, pRefImg1Obj, pRefImg2Obj, pRefTemperatureObj, divWeatherObj, divWindObj);

    CommonAjaxUtils.GetWeatherForecast( function(request){  
        var shWF = request.value;   
        redrawWeather(shWF, pRefObj.showIndex, pRefCityObj, tdBtn1Obj, tdBtn2Obj, tdBtn3Obj, pRefImg1Obj, pRefImg2Obj, pRefTemperatureObj, divWeatherObj, divWindObj);
        } );

    // 绑定第1天的按钮事件
    tdBtn1Obj.onclick = function() {
        pRefObj.showIndex = 0;
        // 重新取得最新天气情况
        // var wfObj = GetWeatherForecast();
        // redrawWeather(wfObj, pRefObj.showIndex, tdBtn1Obj, tdBtn2Obj, tdBtn3Obj, pRefImg1Obj, pRefImg2Obj, pRefTemperatureObj, divWeatherObj, divWindObj);
        CommonAjaxUtils.GetWeatherForecast( function(request){  
            var shWF = request.value;   
            redrawWeather(shWF, pRefObj.showIndex, pRefCityObj, tdBtn1Obj, tdBtn2Obj, tdBtn3Obj, pRefImg1Obj, pRefImg2Obj, pRefTemperatureObj, divWeatherObj, divWindObj);
        } );
    }

    // 绑定第2天的按钮事件
    tdBtn2Obj.onclick = function() {
        pRefObj.showIndex = 1;
        // 重新取得最新天气情况
        // var wfObj = GetWeatherForecast();
        // redrawWeather(wfObj, pRefObj.showIndex, tdBtn1Obj, tdBtn2Obj, tdBtn3Obj, pRefImg1Obj, pRefImg2Obj, pRefTemperatureObj, divWeatherObj, divWindObj);
        CommonAjaxUtils.GetWeatherForecast( function(request){  
            var shWF = request.value;   
            redrawWeather(shWF, pRefObj.showIndex, pRefCityObj, tdBtn1Obj, tdBtn2Obj, tdBtn3Obj, pRefImg1Obj, pRefImg2Obj, pRefTemperatureObj, divWeatherObj, divWindObj);
        } );
    }

    // 绑定第3天的按钮事件
    tdBtn3Obj.onclick = function() {
        pRefObj.showIndex = 2;
        // 重新取得最新天气情况
        // var wfObj = GetWeatherForecast();
        // redrawWeather(wfObj, pRefObj.showIndex, tdBtn1Obj, tdBtn2Obj, tdBtn3Obj, pRefImg1Obj, pRefImg2Obj, pRefTemperatureObj, divWeatherObj, divWindObj);
        CommonAjaxUtils.GetWeatherForecast( function(request){  
            var shWF = request.value;   
            redrawWeather(shWF, pRefObj.showIndex, pRefCityObj, tdBtn1Obj, tdBtn2Obj, tdBtn3Obj, pRefImg1Obj, pRefImg2Obj, pRefTemperatureObj, divWeatherObj, divWindObj);
        } );
    }

    tdCell.appendChild(tblImgObj);
    tdCell.appendChild(tblTextObj);
    tdCell.appendChild(tblBtnObj);

    return tblObj;
}

function redrawWeather(wfObj, showIndex, pRefCityObj, tdBtn1Obj, tdBtn2Obj, tdBtn3Obj, pRefImg1Obj, pRefImg2Obj, pRefTemperatureObj, divWeatherObj, divWindObj) {
    var weather = wfObj.Weathers[showIndex];

    // 设置城市名称
    pRefCityObj.innerHTML = wfObj.Name;
    // 设置城市介绍
    pRefCityObj.title = wfObj.Description;
    
    // 设置第1天阳光指数
    tdBtn1Obj.title = wfObj.Weathers[0].Sunshine + "\r\n\r\n" + wfObj.Weathers[0].Detail;
    
    // 设置按钮背景
    tdBtn1Obj.background = (showIndex==0)?"../../images/weather/sun1.jpg":"../../images/weather/sun2.jpg";
    tdBtn2Obj.background = (showIndex==1)?"../../images/weather/sun1.jpg":"../../images/weather/sun2.jpg";
    tdBtn3Obj.background = (showIndex==2)?"../../images/weather/sun1.jpg":"../../images/weather/sun2.jpg";
    
    // 设置按钮文字
    tdBtn1Obj.innerHTML = wfObj.Weathers[0].ForecastDate;
    tdBtn2Obj.innerHTML = wfObj.Weathers[1].ForecastDate;
    tdBtn3Obj.innerHTML = wfObj.Weathers[2].ForecastDate;

    // 设置天气图片
    pRefImg1Obj.src = (weather.Image1.length==0)?"../../images/weather/none.jpg":"../../images/weather/a_"+weather.Image1;
    pRefImg2Obj.src = (weather.Image2.length==0)?"../../images/weather/none.jpg":"../../images/weather/a_"+weather.Image2;
    // 设置温度
    pRefTemperatureObj.innerHTML = weather.MinTemperature + "～" + weather.MaxTemperature;
    // 设置天气情况
    divWeatherObj.innerHTML = weather.Weather;
    // 设置风力情况
    divWindObj.innerHTML = weather.Wind;
}

function createBusiness(divObj, id, parentObj, pRefObj) {
    // 创建个人代办子窗口
    var tblObj = parentObj.createElement("TABLE");

    tblObj.id = id;
    tblObj.width = pRefObj.width-pRefObj.iconWidth;
    tblObj.border = "0";
    tblObj.cellSpacing = "0";
    tblObj.cellPadding = "0";
    tblObj.style.borderColor = "#fbd9b3";
    tblObj.style.borderStyle = "solid";
    tblObj.style.borderWidth = "2px";
    tblObj.style.lineHeight = "16px";
   
    var rowObj = tblObj.insertRow(-1);
    var celObj = rowObj.insertCell(-1);
    celObj.colSpan = "2";
    celObj.align = "center";
    celObj.style.background = "#fbd9b3";
    
    celObj.innerHTML = "个人消息";
    
    var pcallbackBusiness = function(){
        CommonAjaxUtils.GetBusinessMessage( 
            function (request) {
                var messages=request.value;

                while( tblObj.rows.length>1 )
                    tblObj.deleteRow(1);

                for(var i=0; i<messages.length; i++) {
                    var lnkObj = parentObj.createElement("A");
                    lnkObj.href = "javascript:top.window.topFrame.getNewMessage();window.open('../Input/MessageReadForm.aspx?MESSAGE_ID=" + messages[i].MessageId +"','_blank','scrollbars,resizable,height=550,width=520');void(0);";
                    lnkObj.title = messages[i].Message;
                    
                    rowObj = tblObj.insertRow(-1);
                    celObj = rowObj.insertCell(-1);
                    celObj.width = "220";
                    lnkObj.innerHTML = ((messages[i].TargetEmpName==null)?"":messages[i].TargetEmpName) + "：" + messages[i].Subject.substring(0,8);
                    celObj.appendChild( lnkObj );
                    celObj = rowObj.insertCell(-1);
                    celObj.width = "30";
                    celObj.innerHTML = messages[i].NewDate.format("yyyy/MM/dd");
                }
                
                var lnkObj = parentObj.createElement("A");
                lnkObj.href = "../Input/MessageSrhForm.aspx";
                lnkObj.innerHTML = "更多...";

                rowObj = tblObj.insertRow(-1);
                celObj = rowObj.insertCell(-1);
                celObj.colSpan = "2";
                celObj.align = "right";
                celObj.appendChild( lnkObj );

                pRefObj.normalWindow();
            } ) 
        };

    pcallbackBusiness();
    
    pRefObj.businessTimerId = window.setInterval(
      pcallbackBusiness
    , 15000);

    return tblObj;
}

function normalWindow() {
    // 重新调整大小
    var iconWidth = this.iconWidth;
    var bodyWidth = this.width;
    var clientHeight = this.parentObj.body.clientHeight;
    var clientWidth = this.parentObj.body.clientWidth;

    this.floatObj.style.left = clientWidth-((this.isIconic)?iconWidth:bodyWidth);
    this.floatObj.style.width = (this.isIconic)?iconWidth:bodyWidth;
    this.floatObj.style.height = clientHeight;
}

function FloatBar(id) {
    this.id = id;
    this.iconWidth = 20;
    this.width = 250;
    this.isIconic = !false;
    this.background = '#ffffff';

    this.parentObj = null;
    this.floatObj = null;
    this.ctxObj = null;

    this.createFloatBar = createFloatBar;
    this.drawFloatBar = drawFloatBar;
    this.drawContext = drawContext;
    this.normalWindow = normalWindow;
    this.showIndex = 0;
}

function drawFloatBar() {
    try {
        var iconWidth = this.iconWidth;
        var bodyWidth = this.width;
        var clientHeight = this.parentObj.body.clientHeight;
        var clientWidth = this.parentObj.body.clientWidth;
        
        this.floatObj.style.left = clientWidth-((this.isIconic)?iconWidth:bodyWidth) + this.parentObj.body.scrollLeft;
        this.floatObj.style.top = this.parentObj.body.scrollTop;
        this.floatObj.style.width = (this.isIconic)?iconWidth:bodyWidth;
        this.floatObj.style.height = clientHeight;
        this.floatObj.style.background = this.background;
        
        var imgArrowObj = this.parentObj.getElementById("IMG_"+this.id);
        if( imgArrowObj!=null ) {
            imgArrowObj.src = ((this.isIconic)?"../../images/float_bar_l_arrow.gif":"../../images/float_bar_r_arrow.gif");
        }

        // 通知主窗口浮动条状态已改变, isIconic: True - 是缩小状态, False - 是展开状态
        if( typeof(this.parentObj.parentWindow.onNotifyFloatBarChange)=='function' ) {
            this.parentObj.parentWindow.onNotifyFloatBarChange(this.isIconic);
        }
    }
    catch(e) {
        alert(e.message);
    }
}

function drawContext() {
    var wid = this.id+"_Weather1";
    var bus = this.id+"_Business1";

    var parentObj = this.parentObj;
    var ctxObj = this.ctxObj;
    var floatObj = this.floatObj;
    
    try {
        if( this.isIconic==false && parentObj.getElementById(wid)==null ) {
            // 未绘制天气预报的情况绘制处理
            ctxObj.appendChild( createWeather(floatObj, wid, parentObj, this) );
            // 添加个人代办
            ctxObj.appendChild( createBusiness(floatObj, bus, parentObj, this) );
        }
    }
    catch(e) {
        alert(e.message);
    }
}

function createFloatBar(parentObj) {
    var iconWidth = this.iconWidth;
    var bodyWidth = this.width;
    var clientHeight = parentObj.body.clientHeight;
    var clientWidth = parentObj.body.clientWidth;

    var divObj = parentObj.createElement("DIV");
    divObj.style.position = "absolute";
    divObj.style.overflowX = "hidden";
    divObj.style.top = 0;
    parentObj.body.appendChild(divObj);

    // 绘制控件
    var tblFrameObj = parentObj.createElement('TABLE');
    tblFrameObj.width = this.width;
    tblFrameObj.height = "100%";
    tblFrameObj.border = "0";
    tblFrameObj.cellSpacing = "0";
    tblFrameObj.cellPadding = "0";
    
    var tbRow = tblFrameObj.insertRow(-1);

    // 浮动条控制区域
    var imgArrowObj = parentObj.createElement("IMG");
    imgArrowObj.id = "IMG_"+this.id;
    imgArrowObj.src = "../../images/float_bar_r_arrow.gif";
    var tbCtlObj = tbRow.insertCell(-1);
    tbCtlObj.width = this.iconWidth;
    tbCtlObj.align = "center";
    tbCtlObj.vAlign = "center";
    tbCtlObj.background = "../../images/float_bar_bg.jpg";
    tbCtlObj.style.cursor = "hand";
    tbCtlObj.appendChild(imgArrowObj);

    // 浮动内容显示区域
    tbCntObj = tbRow.insertCell(-1);
    tbCntObj.width = this.width-this.iconWidth;
    
    tbCntObj.vAlign = "top";
    
    // 设置父窗口对象
    this.parentObj = parentObj;
    // 设置浮动条对象
    this.floatObj = divObj;
    // 浮动条内容对象
    this.ctxObj = tbCntObj;

    divObj.appendChild(tblFrameObj);

    pThisRef = this;

    this.drawFloatBar();
    this.drawContext();

    var pFloatClickFunc = function() {
            pThisRef.isIconic = !pThisRef.isIconic;
            // 绘制控件
            pThisRef.drawFloatBar();
            // 绘制子窗口
            pThisRef.drawContext();
        } 

    var pResizeFunc = function() {
            // 绘制控件
            pThisRef.drawFloatBar();
        } 

    if (document.all)  {
        tbCtlObj.attachEvent("onclick", pFloatClickFunc);
        parentObj.parentWindow.top.attachEvent("onresize", pResizeFunc);
        parentObj.parentWindow.top.frames[1].attachEvent("onscroll", pResizeFunc);
        // 页面离开时必须撤销注册的onresize事件
        parentObj.parentWindow.top.frames[1].attachEvent("onunload",  function() {parentObj.parentWindow.top.detachEvent("onresize", pResizeFunc); window.clearInterval(pThisRef.businessTimerId); } );
    }
    else {
        tbCtlObj.addEventListener("click", pFloatClickFunc, false);
        parentObj.defaultView.top.addEventListener("resize", pResizeFunc, false);
    }
}